package com.musicserver.service.Impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.musicserver.common.R;
import com.musicserver.model.domain.Singer;
import com.musicserver.model.request.SingerRequest;
import com.musicserver.service.SingerService;
import com.musicserver.mapper.SingerMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
* @author tch001
* @description 针对表【singer】的数据库操作Service实现
* @createDate 2025-09-15 14:59:03
*/
@Service
public class SingerServiceImpl extends ServiceImpl<SingerMapper, Singer>
    implements SingerService{

    @Autowired
    private SingerMapper singerMapper;

    @Override
    public R allSinger(int curPage, int pageSize) {
        Page<Singer> result = singerMapper.selectPage(new Page<>(curPage, pageSize), null);
        return R.success(null,result.convert(singer ->{
            SingerRequest singerRequest = new SingerRequest();
            BeanUtils.copyProperties(singer, singerRequest);
            return singerRequest;
        }));
    }

    @Override
    public R addSinger(SingerRequest singerRequest) {
        Singer singer = new Singer();
        BeanUtils.copyProperties(singerRequest, singer);
        //给出默认头像,后续管理员可以在线修改
        String pic = "/img/avatorImages/user.jpg";
        singer.setPic(pic);
        if (singerMapper.insert(singer) > 0) {
            return R.success("添加成功");
        } else {
            return R.error("添加失败");
        }
    }

    @Override
    public R deleteSinger(int id) {
        if (singerMapper.deleteById(id) > 0) {
            return R.success("删除成功");
        } else {
            return R.error("删除失败");
        }
    }

    @Override
    public R deleteSingers(String[] ids) {
        //把数组中参数组织成SQL语句需要的格式
        StringBuffer sb = new StringBuffer();
        for (String i:ids) {
            sb.append(i+",");
        }
        sb.deleteCharAt(sb.length()-1);
        if (singerMapper.deleteByIds(sb.toString()) > 0) {
            return R.success("删除成功");
        } else {
            return R.error("删除失败");
        }
    }

    @Override
    public R allSingers() {
        return R.success(null, singerMapper.selectList(null));
    }


    @Override
    public R updateSingerMsg(SingerRequest singerRequest) {
        Singer singer = new Singer();
        BeanUtils.copyProperties(singerRequest, singer);
        if (singerMapper.updateById(singer) > 0) {
            return R.success("更新成功");
        } else {
            return R.error("更新失败");
        }
    }



}




